package com.lyj.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyj.mall.consts.MethodConst;
import com.lyj.mall.service.ProductService;
import com.lyj.mall.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lyj
 * @date 2020/1/8 23:10
 */
@Api(tags = "商品模块Controller")
@RestController
@RequestMapping(value = "/products")
@Slf4j
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(value = "categoryId", name = "商品类型id"),
            @ApiImplicitParam(value = "pageNum", name = "页"),
            @ApiImplicitParam(value = "pageSize", name = "条")
    })
    @ApiOperation(value = "根据类目id查询该类商品", httpMethod = "GET")
    public ResponseVo<IPage> list(@RequestParam(value = "categoryId", required = false) Integer categoryId,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        this.insertLog("根据类目id查询商品", MethodConst.QUERY, StrUtil.format("categoryId:{},pageNum:{},pageSize:{}", categoryId, pageNum, pageSize));
        ResponseVo<IPage> pageResponseVo = productService.list(categoryId, pageNum, pageSize);
        return pageResponseVo;
    }

    @GetMapping("/{productId}")
    @ApiImplicitParam(value = "productId", name = "商品id")
    @ApiOperation(value = "根据商品id查询商品", httpMethod = "GET")
    public ResponseVo detail(@PathVariable("productId") Integer productId) {
        this.insertLog("根据商品id查询商品", MethodConst.QUERY, "productId: " + productId);
        return productService.detail(productId);
    }
}
